const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

function request( url,data={},method="POST"){
    return new Promise((resolve,reject)=>{
        wx.request({
            url:url,
            data:data,
            method:method,
            header:{
                token:wx.getStorageSync('token')
            },
            success:(res)=>{
                if (res.data.code ==200) {
                    resolve(res)
                }else if(res.data.code ==401){
                    wx.showModal({
                      title: '提示',
                      content: '您的登录已失效请重新登录',
                      complete: (res) => {
                        if (res.confirm) {
                          console.log('用户点击确定');
                          wx.navigateTo({
                            url: '/pages/index/index',
                          })
                        }else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                      }
                    })
                }
            },
            fail:(err)=>{
                reject(err)
            },
        })
    })
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  request
  
}
